#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tmf_js_func.py 
@version:
@time: 2022/06/21 
"""
from TapClientCore.T02_tm_api.v2_api.v2_tm import v2tm as TM


class TMFJsFunc:

    # 查询
    @staticmethod
    def tm_get_js_func(_filter):
        return TM.get_javascript_function(_filter).get('data')

    # 创建
    @staticmethod
    def tm_post_js_func(payload):
        return TM.post_javascript_function(payload)

    # 修改
    @staticmethod
    def tm_patch_js_func(payload):
        return TM.patch_javascript_function(payload)

    # 删除
    @staticmethod
    def tm_delete_js_func(func_id):
        return TM.delete_javascript_function(func_id)

    # 上传jar包
    @staticmethod
    def tm_post_file_upload(file_path):
        return TM.post_file_upload(file_path)

    # 通过名字查询
    def tmf_get_js_func_by_name(self, function_name):
        limit = 20
        skip = 0
        while True:
            _filter = {
                'where': {
                    'type': {'inq': ['custom', 'jar']},
                },
                'order': 'createTime DESC',
                'limit': limit,
                'skip': skip
            }
            response = self.tm_get_js_func(_filter)
            items = response.get('items')
            if not items:
                break
            else:
                for one in items:
                    if one.get('function_name') == function_name:
                        return one
            skip += limit

    # 通过名字删除
    def tmf_delete_js_func_by_name(self, function_name):
        func_id = self.tmf_get_js_func_by_name(function_name).get('id')
        return self.tm_delete_js_func(func_id)

    # 通过名字修改
    def tmf_patch_func_by_name(self, function_name, patch):
        result = self.tmf_get_js_func_by_name(function_name)
        result.update(patch)
        return self.tm_patch_js_func(result)

    # 创建
    def tmf_create_func(self, payload):
        return self.tm_post_js_func(payload)


if __name__ == '__main__':
    T = TMFJsFunc()
    res = T.tmf_get_js_func_by_name('RobotJS')
    print(res)
